package com.ruoyi.job.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.goods.api.RemoteGoodsService;
import com.ruoyi.goods.api.domain.BmPreferential;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Author {那蓝桉}
 * @Date: 2023/08/12/ 14:56
 * @description
 */
@Component("seckillTask")
public class SeckillTask {
    @Resource
    private RedisService redisService;
    @Resource
    private RemoteGoodsService remoteGoodsService;
    @Scheduled(cron = "0 0 1 * * ?") // 每天凌晨1点执行一次
    public void execute() {
//        删除之前的数据
        String key = "goodsList:queryAll";
        Boolean b = redisService.hasKey(key);
        if(b) {
            redisService.deleteObject(key);
        }
//        查询当天的商品信息
        AjaxResult result = remoteGoodsService.queryByStartTime();
        List<BmPreferential> list = JSONArray.parseArray(JSON.toJSONString(result.get("data")), BmPreferential.class);
//        将当天的秒杀商品状态设为上架
        for (BmPreferential bmPreferential : list) {
            // 对BmPreferential对象进行操作
            remoteGoodsService.updateStatus(bmPreferential.getId());
        }
//        查询当天所有上架的秒杀商品，并存入redis中
        remoteGoodsService.queryAll();
    }
}
